/**
 * 
 */
package TelitPy;

import COMM232.DriverSerial;

/**
 * @author Emerson Beserra
 *
 */
public class Packet
{
	
	/**
	 * SOH - start of heading.
	 */
	public static final byte beginPack = 0x01;
	
	/**
	 * ETB - End of Block.
	 */
	private static final byte endPack   = 0x17;
	
	/**
	 * 
	 */
	private char payload[];
	
	/**
	 * 
	 */
	private char checksum;
	
	/**
	 * 
	 */
	private char packLenght;
	
	/**
	 * 
	 */
	private DriverSerial driver;

	public Packet( int data[] , DriverSerial driver )
	{
		packLenght =  256; 
		payload = new char[packLenght];
		checksum = 0;
		makePacket(data);
		this.driver = driver;
	}//end Packet()-------------------------------------------------------------
	
	public Packet( int packLenght, int data[] , DriverSerial driver )
	{
		if( packLenght > 256 )
			this.packLenght =  256;
		else
			this.packLenght =  (char) packLenght; 
		payload = new char[packLenght];
		checksum = 0;
		makePacket(data);
		this.driver = driver;
	}//end Packet()-------------------------------------------------------------
	
	public Packet( int packLenght, char data[] , DriverSerial driver )
	{
		if( packLenght > 256 )
			this.packLenght =  256;
		else
			this.packLenght =  (char) packLenght; 
		payload = new char[packLenght];
		checksum = 0;
		makePacket(data);
		this.driver = driver;
	}//end Packet()-------------------------------------------------------------
	
	private void makePacket( int data[] )
	{
		String dado;
		System.out.println("dados do pacote:");
		for( int i = 0; i < packLenght; i++)
		{
			payload[i] = (char) data[i];
			dado = Integer.toHexString( (int) payload[i]);
			System.out.print(dado + ", ");
			//System.out.println((int) payload[i]);
			checksum += data[i];
		}//end for
		System.out.println("fim de pacote");
	}//end makePacket()---------------------------------------------------------
	
	private void makePacket( char data[] )
	{
		String dado;
		System.out.println("dados do pacote:");
		for( int i = 0; i < packLenght; i++)
		{
			payload[i] = data[i];
			dado = Integer.toHexString( (int) payload[i]);
			System.out.print(dado + ", ");
			//System.out.println((int) payload[i]);
			checksum += data[i];
		}//end for
		System.out.println("fim de pacote");
	}//end makePacket()---------------------------------------------------------
	
	public void sendPacket()
	{
		char pct[] = new char[packLenght + 6];
		pct[0] = Packet.beginPack;
		pct[1] = Packet.beginPack;
		pct[2] = packLenght;
		pct[pct.length - 1] = Packet.endPack;
		pct[pct.length - 2] = Packet.endPack;
		pct[pct.length - 3] = checksum;
		
		for(int i = 0; i < packLenght; i++)
			pct[i + 3] = payload[i];
		
		driver.sendBytes(pct);
		
//		driver.sendByte(Packet.beginPack);
//		//System.out.println(Packet.beginPack);
//		driver.sendByte(Packet.beginPack);
//		//System.out.println(Packet.beginPack);
//		driver.sendByte(packLenght);
//		System.out.println("packLenght: " + packLenght);
//		driver.sendBytes(payload);
//		//System.out.println(" payload ");
//		checksum = (char) (0x00FF & checksum);
//		driver.sendByte(checksum);
//		System.out.println(Integer.toHexString( (int)checksum));
//		driver.sendByte(Packet.endPack);
//		//System.out.println(Packet.endPack);
//		driver.sendByte(Packet.endPack);
//		//System.out.println(Packet.endPack);
	}//end sendPacket()---------------------------------------------------------
	
}
